package com.sse.task;

import com.alibaba.fastjson.JSONObject;
import com.sse.context.SseSessionHolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;

import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @title: LiveOilPricePushServer
 * @Author xu-xiang
 * @Date: 2024/1/26 15:50
 * @description: 实时油价推送
 */
//@Service
public class LiveOilPricePushServer implements ScheduledPushServer {

    private static final Logger logger = LoggerFactory.getLogger(LiveOilPricePushServer.class);


    @Override
    public Integer getScheduledTime() {
        return 3;
    }

    @Override
    public TimeUnit getTimeUnit() {
        return TimeUnit.SECONDS;
    }

    @Override
    public void run() {
        //模拟97#油价
        JSONObject oil97Price = new JSONObject();
        oil97Price.put("country", "China");
        oil97Price.put("pricePerLiter", 7 + new Random().nextDouble() * 2);
        SseSessionHolder.sendAll(oil97Price.toJSONString(), MediaType.APPLICATION_JSON);
    }
}
